﻿using OfficeOpenXml;
using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ExcelFileQueryTool
{
    public partial class frmEditHeader : Form
    {
        public frmEditHeader()
        {
            InitializeComponent();
        }

        private bool HasFileReady()
        {
            var file = txtFile.Text.Trim();

            return File.Exists(file);
        }

        private void txtFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog diag = new OpenFileDialog())
            {
                diag.Filter = AppConfig.ClientFileChooseFilter;
                diag.InitialDirectory = AppConfig.SourceFolder;

                if (diag.ShowDialog(this) == DialogResult.OK)
                    txtFile.Text = diag.FileName;
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (HasFileReady() == false)
                return;

            using (ExcelPackage package = new ExcelPackage(new FileInfo(this.txtFile.Text)))
            {
                foreach (ucSheetHeader header in this.panelHeaders.Controls)
                {
                    if (header.HasChanged == false)
                        continue;

                    var sheet = package.Workbook.Worksheets[header.SheetName];

                    sheet.HeaderFooter.OddHeader.CenteredText = header.SheetTitle;

                    var contactInfo = string.Format(AppConfig.ClientFileContactInfoFormat, header.ContactPerson, header.Phone, header.Address);
                    sheet.HeaderFooter.OddHeader.LeftAlignedText = Regex.Replace(sheet.HeaderFooter.OddHeader.LeftAlignedText, AppConfig.ClientFileContactInfoRegexPattern, contactInfo); ;
                }

                package.Save();
            }

            MessageDispatcherManager.DispatchMessage(
                            new AppMessage
                            {
                                Time = DateTime.Now,
                                Level = AppMessageLevel.Info,
                                Title = "编辑表头操作",
                                Content = "保存成功！"
                            });

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtFile_TextChanged(object sender, EventArgs e)
        {
            if (HasFileReady() == false)
                return;

            this.panelHeaders.Controls.Clear();

            using (ExcelPackage package = new ExcelPackage(new FileInfo(this.txtFile.Text)))
            {
                var x = 0;
                var y = 0;

                foreach (var sheet in package.Workbook.Worksheets)
                {
                    if (sheet.IsHistorySheet())
                        continue;

                    if (sheet.IsDataSheet())
                    {
                        var infoArr = ClientFileService.RetriveContactInfo(sheet.HeaderFooter.OddHeader.LeftAlignedText);
                        if (infoArr == null) // there is no contact information in the sheet,just ignore it
                            continue;

                        var title = sheet.HeaderFooter.OddHeader.CenteredText;

                        var header = new ucSheetHeader(sheet.Name, title, infoArr[0], infoArr[1], infoArr[2]);
                        header.Location = new Point(x, y);

                        this.panelHeaders.Controls.Add(header);

                        y = y + header.Height + 10;
                    }
                }
            }

            this.btnCopyFromFirst.Enabled = this.panelHeaders.Controls.Count > 1;
        }

        private void btnCopyFromFirst_Click(object sender, EventArgs e)
        {
            var firstHeader = (ucSheetHeader)this.panelHeaders.Controls[0];

            for (int i = 1; i < this.panelHeaders.Controls.Count; i++)
            {
                // ignore title
                var header = (ucSheetHeader)this.panelHeaders.Controls[i];
                header.ContactPerson = firstHeader.ContactPerson;
                header.Phone = firstHeader.Phone;
                header.Address = firstHeader.Address;
            }
        }
    }
}
